package com.example.keli.ui.my.bindinfo;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.app.C;
import com.example.keli.base.utils.ToastUtil;
import com.example.keli.ui.LoginActivity;
import com.example.keli.ui.register.VerifyActivity;
import com.flyco.roundview.RoundTextView;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * description 绑定手机
 * created by jerry on 2019/3/18.
 */
public class VerifyNumberActivity extends BaseActivity {

    @BindView(R.id.tv_next)
    RoundTextView tvNext;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.tv_verify_code)
    TextView tvVerifyCode;
    @BindView(R.id.et_code)
    EditText etCode;

    @Override
    protected int layoutId() {
        return R.layout.activity_bind_verify;
    }

    @Override
    protected void bindData() {
//        setTitle("绑定手机");
        abTitle.setText("绑定手机");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvNext.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.tv_verify_code, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_verify_code:
                if ("获取验证码".equals(tvVerifyCode.getText().toString())){
                    if(TextUtils.isEmpty(etNumber.getText().toString().trim())){
                        ToastUtil.show("请输入手机号");
                        return;
                    }
                    sendSms();
                }
                break;
            case R.id.tv_next:
                if(TextUtils.isEmpty(etCode.getText().toString().trim())){
                    ToastUtil.show("请输入验证码");
                    return;
                }
                bingPhone();
                break;
        }
    }

    private void sendSms() {
        Map<String, String> map = new HashMap<>();
        map.put("address", etNumber.getText().toString().trim());
        map.put("type", "updateUser");
        HttpUtils.ApiFunc(RetrofitManager.mApiService.sendSms(map), new SubscriberListener<BaseResponse<Object>>() {
            @Override
            public void onSuccess(BaseResponse<Object> rewardListBaseResponse) {
//                stopProgressDialog();
                tvVerifyCode.setText("验证码已发送");
                ToastUtil.show("验证码已发送");
            }

            @Override
            public void onFailer(String msg) {
//                stopProgressDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onTokenError() {

            }
        });
    }

    private void bingPhone() {
        Map<String, String> map = new HashMap<>();
        map.put("phone", etNumber.getText().toString().trim());
        map.put("code", etCode.getText().toString().trim());
        RetrofitManager.ApiFunc(RetrofitManager.mApiFunc.setUser(map), new SubscriberListener<BaseResponse<Object>>() {
            @Override
            public void onSuccess(BaseResponse<Object> objectBaseResponse) {
                ToastUtil.show("绑定成功");
                EventBus.getDefault().post(C.EVENT.UPDATE_USERINFO);
                finish();
            }

            @Override
            public void onFailer(String msg) {
                ToastUtil.show(msg);
            }

            @Override
            public void onTokenError() {

            }
        });
    }
}
